package com.tianruan.psychological.moduls.sys.user.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianruan.psychological.common.base.controller.BaseController;
import com.tianruan.psychological.common.constant.GlobalExceptionEnum;
import com.tianruan.psychological.common.response.VResponse;
import com.tianruan.psychological.moduls.sys.user.entry.dto.UserDTO;
import com.tianruan.psychological.moduls.sys.user.entry.vo.UserVO;
import com.tianruan.psychological.moduls.sys.user.service.IUserService;
import com.tianruan.psychological.moduls.sys.user.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * @author liuxin
 * @date 2019/10/13
 */
@RestController
@RequestMapping("/api/sys/user")
@Slf4j
@Api(description = "用户接口")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @PreAuthorize("hasAuthority('user-edit')")
    @ApiOperation(value = "获取用户", notes = "分页获取用户")
    @GetMapping
    public ResponseEntity pageUser(Page page,
                                   @RequestParam(value = "no",required = false)String no,
                                   @RequestParam(value = "username",required = false)String username,
                                   @RequestParam(value = "phone",required = false)String phone,
                                   @RequestParam(value = "email",required = false)String email,
                                   @RequestParam(value = "role",required = false)String role){
        return ResponseEntity.ok(userService.pageUser(page, no, username, phone, email, role));
    }

    @ApiOperation(value = "获取用户", notes = "根据用户id获取用户")
    @GetMapping("/{id}")
    public ResponseEntity getUser(@PathVariable("id")String id){
        UserVO userRoleById = userService.getUserRoleById(id);
        return ResponseEntity.ok(userRoleById);
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户标识", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping
    public ResponseEntity insertUser(@RequestBody UserDTO userDTO){
        return ResponseEntity.ok(userService.insertUser(userDTO));
    }

    /**
     * 更新用户信息
     * @param id
     * @param userDTO
     * @return
     */
    @PutMapping("/{id}")
    public ResponseEntity updateUser(@PathVariable("id")String id,
                                     @RequestBody UserDTO userDTO){
        return ResponseEntity.ok(userService.updateUser(id, userDTO));
    }

    @GetMapping("/self")
    public VResponse<UserVO> self(){
        UserDTO userDTO = UserUtils.getUser();
        UserVO userInfo = new UserVO();
        try {
            BeanUtils.copyProperties(userInfo, userDTO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return VResponse.success(userInfo);
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     */
    @PutMapping("/modify/password")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public VResponse<Object> modifyPassword(@RequestParam("oldPassword") String oldPassword,
                                            @RequestParam("newPassword") String newPassword) {
        UserDTO user = UserUtils.getUser();
        Boolean equals = userService.equalsPassword(user.getId(), oldPassword);
        if (equals) {
            userService.modifyPassword(user.getId(), newPassword);
            return VResponse.success(GlobalExceptionEnum.MODIFY_PASSWORD_SUCCESS);
        } else {
            return VResponse.success(GlobalExceptionEnum.ERROP_PASSWORD);
        }
    }
}
